package com.grow.greenhouse.controller;

import com.grow.greenhouse.common.BaseResponse;
import com.grow.greenhouse.common.ErrorCode;
import com.grow.greenhouse.domain.Systemthreshold;
import com.grow.greenhouse.domain.request.DailyNumRequest;
import com.grow.greenhouse.domain.request.UpdateAllThresholdRequest;
import com.grow.greenhouse.domain.request.UpdateOneThresholdRequest;
import com.grow.greenhouse.exception.BusinessException;
import com.grow.greenhouse.service.UserthreholdService;
import com.grow.greenhouse.utils.ResultUtils;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/threshold")
public class ThresholdController {

    @Resource
    private UserthreholdService userthreholdService;
    @ApiOperation("当日数据页获取阈值")
    @PostMapping("/getone")
    public BaseResponse<Map<String,String>> getOneThreshold(@RequestBody DailyNumRequest dailyNumRequest,
                                                       HttpServletRequest request)
    {
        if(dailyNumRequest == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"指标名不得为空");
        }
        Map<String,String> threshold =userthreholdService.getOneThreshold(dailyNumRequest,request);
        return ResultUtils.success(threshold);
    }


    @ApiOperation("获取所有的阈值")
    @GetMapping("/all")
    public BaseResponse<List<Map<String,String>>> getAllThreshold(@RequestParam String shedName,
                                                                  HttpServletRequest request)
    {
        if(StringUtils.isBlank(shedName))
        {
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"大棚名称不能为空");
        }
        List<Map<String,String>> thresholds = userthreholdService.getAllThreshold(shedName,request);
        return ResultUtils.success(thresholds);
    }

    @ApiOperation("更新单个阈值")
    @PostMapping("/update")
    public BaseResponse<String> updateThreshold(@RequestBody UpdateOneThresholdRequest thresholdRequest,
                                           HttpServletRequest request)

    {
        if(thresholdRequest == null)
        {
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"修改参数不能为空");
        }
        String update = userthreholdService.updateThreshold(thresholdRequest,request);
        return ResultUtils.success(update);
    }

    @ApiOperation("更新所有阈值")
    @PostMapping("/update/all")
    public BaseResponse<String> updateAllThreshold(@RequestBody UpdateAllThresholdRequest thresholdRequest,
                                                   HttpServletRequest request)
    {
        if(thresholdRequest == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"阈值参数为空");
        }
        String update = userthreholdService.updateAllThreshold(thresholdRequest,request);
        return ResultUtils.success(update);
    }


    @ApiOperation("获取当前系统阈值中包含的作物名")
    @GetMapping("/get/system")
    public BaseResponse<List<String>> getSystemCropName()
    {
        List<String> cropList = userthreholdService.getSystemCropName();
        return ResultUtils.success(cropList);
    }
}
